package com.data.modules.message.websocket;

import com.data.common.constant.WebsocketConst;
import com.data.modules.message.entity.SysMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.data.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
@Api(tags="webSocket")
@RestController
@RequestMapping("/api2021/socketTest")
public class TestSocketController {

    @Autowired
    private WebSocket webSocket;

	@ApiOperation(value="sendAll", notes="sendAll")
    @PostMapping("/sendAll")
    public Result<String> sendAll(@RequestBody JSONObject jsonObject) {
    	Result<String> result = new Result<String>();
    	String message = jsonObject.getString("message");
    	JSONObject obj = new JSONObject();
    	obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_TOPIC);
		obj.put(WebsocketConst.MSG_ID, "M0001");
		obj.put(WebsocketConst.MSG_TXT, message);
    	webSocket.sendMessage(obj.toJSONString());
        result.setResult("群发！");
        return result;
    }
	//curl -X POST -H  "Accept:*/*" -H  "Content-Type:application/json" -d "{\"userId\":\"15392\",\"message\":\"good\"}" "http://123.56.42.177:32530/jeecg-boot/api2021/socketTest/sendUser"
	@ApiOperation(value="sendUser", notes="sendUser")
    @PostMapping("/sendUser")
    public Result<String> sendUser(@RequestBody JSONObject jsonObject) {
    	Result<String> result = new Result<String>();
    	String userId = jsonObject.getString("userId");
    	String message = jsonObject.getString("message");
    	JSONObject obj = new JSONObject();
    	obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_USER);
    	obj.put(WebsocketConst.MSG_USER_ID, userId);
		obj.put(WebsocketConst.MSG_ID, "M0001");
		obj.put(WebsocketConst.MSG_TXT, message);
        webSocket.sendMessage(userId, obj.toJSONString());
        result.setResult("单发");
        return result;
    }

	@ApiOperation(value="sendAI", notes="sendAI")
	@PostMapping("/sendAI")
	public Result<String> sendAI(@RequestBody JSONObject jsonObject) {
		Result<String> result = new Result<String>();
		String userId = jsonObject.getString("userId");
		webSocket.sendMessage(userId, jsonObject.toJSONString());
		result.setResult(userId + "sendAI OK");
		return result;
	}

	/**
	 * 给指定 自动化部署agent 发送部署命令
	 *
	 * @param appName
	 * @return
	 */
	@ApiOperation(value="sendCD", notes="sendCD")
	@PostMapping("/sendCD")
	public Result<String> sendCD(@RequestBody String appName) {
		//TODO　防止短时间内重复发版　需要将消息持久化至数据库　并交由SendCDJob 处理
		/*SysMessage cicd = new SysMessage();
		cicd.setEsContent(appName);
		cicd.setEsTitle(appName);
		cicd.setEsType(WebsocketConst.CMD_CD);
		cicd.setEsReceiver("CD_1000");*/

		String userId ="CD_1000";
		JSONObject obj = new JSONObject();
		obj.put(WebsocketConst.MSG_CMD, WebsocketConst.CMD_CD);
		obj.put(WebsocketConst.MSG_USER_ID, userId);
		//obj.put(WebsocketConst.MSG_ID, id);
		obj.put(WebsocketConst.MSG_TXT, appName);


		Result<String> result = new Result<String>();
		//直接推送部署命令
		webSocket.sendMessage(userId, obj.toJSONString());
		result.setResult(appName + "sendCD OK");
		return result;
	}



}
